/**
 * 
 */
package br.edu.ufcg.embedded.util;

/**
 * @author daniel
 *
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Parser {
	
	private BufferedReader reader;
	private MultiFileGenerator multiFileGenerator;
	private StringBuilder sb;
	
	public Parser()
	{
	}
	
	public void setFileGenerator(MultiFileGenerator fileGenerator)
	{
		this.multiFileGenerator = fileGenerator;
	}
	
	public void parse(String file, String delimiter) throws IOException {
		
		reader = new BufferedReader(new FileReader(file));
		String line = null;
		
		while ((line = reader.readLine()) != null) {
			String[] contents = line.split(delimiter);
			
			multiFileGenerator.write(format(contents));
			
			line = null;
			contents = null;
		}
		
		multiFileGenerator.close();
		
		if (reader != null)
			reader.close();
		
		reader = null;
		multiFileGenerator = null;
	}
	
	private String format(String[] contents) {
		
		sb = null;
		sb = new StringBuilder();
		
		if (contents != null && contents.length > 0) {
			
			sb.append(contents[1]);
			sb.append(";");
			sb.append(contents[0]);
		}
		
		contents = null;
		
		return sb.toString();	
	}
}